package sample;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import java.sql.PreparedStatement;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MessageDao<puibic> {
    private static String dbUser = "root";
    private static String dbPassword = "7878478";
    private static String dbName = "yiqing";
    private static String coonUrl = "jdbc:mysql://localhost:3306/yiqing?characterEncoding=utf-8&useUnicode=true&serverTimezone=UTC";
    private  static Connection conn;
public MessageDao(){
    conn = DbUtil.getMySQLConnection(coonUrl,dbUser,dbPassword);
}


    public static Message getSqlConnection(String city) throws SQLException, UnsupportedEncodingException {

        Statement statement = conn.createStatement();
        PreparedStatement ps=null;
//        String urlStr = URLEncoder.encode(city,"UTF-8");
//        String  geturl = "https://qqlykm.cn/api/yiqing/?key=6i9Khjgl7RlqRQHHU0tmhGknvg&city="+urlStr;
//        String result1 = HttpUtil.get(geturl);
//        System.out.println(result1);
//        String result2 = result1.substring(0,result1.length()-1).replace("{\"success\":true,\"data\":","");
//        Message message_1 =  JSONUtil.toBean(result2, Message.class);
        Message message_1 = new Message();
        message_1.setName(city);
        String j = city;
        if(isExist(city)){
            String sql = "select * from information where name ="+"\""+city+"\"";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                    message_1.setName(rs.getString("name"));
                    message_1.setConNum(rs.getString(2));
                    message_1.setDeathNum(rs.getString(3));
                    message_1.setCureNum(rs.getString(4));
                    message_1.setAsymptomNum(rs.getString(5));
                    message_1.setUpdateTime(rs.getString(6));


                System.out.println("测试是ggg否允许");
            }
            rs.close();
            statement.close();
            System.out.println("测试zzz是否运行");
            return message_1;

        }else {
            String urlStr = URLEncoder.encode(city,"UTF-8");
            String  geturl = "https://qqlykm.cn/api/yiqing/?key=6i9Khjgl7RlqRQHHU0tmhGknvg&city="+urlStr;
            String result1 = HttpUtil.get(geturl);
            String result2 = result1.substring(0,result1.length()-1).replace("{\"success\":true,\"data\":","");
            message_1 =  (Message) JSONUtil.toBean(result2, Message.class);
            String str3 = message_1.getCureNum();
            if(str3.contains("c")){
                message_1.setCureNum(message_1.getCureNum().substring(0,str3.indexOf("\"")));
            }

            String sql2 = "insert into information(name,conNum,deathNum,cureNum,asymptomNum,updateTime) values(?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql2);
            ps.setString(1,message_1.getName());
            ps.setString(2,message_1.getConNum());
            ps.setString(3,message_1.getDeathNum());
            ps.setString(4,message_1.getCureNum());
            ps.setString(5,message_1.getAsymptomNum());
            ps.setString(6,message_1.getUpdateTime());
            ps.executeUpdate();
            System.out.println("测试是否运行");

            ps.close();

            return message_1;
        }

    }
    public static boolean isExist(String city) throws SQLException {
    Statement statement = conn.createStatement();
 //   String sql = "SELECT name,updateTime from information where `name`=\""+city+"\" "+"and"+" locate("+"\""+data+"\""+",updateTime)";
//    System.out.println(sql);
        String sql = "SELECT name from information where `name`=\""+city+"\"";
    ResultSet rs = statement.executeQuery(sql);
    Message message_2 = new Message();
    while(rs.next()){
        message_2.setName(rs.getString("name"));
    }
    if(message_2.getName()==null){

        rs.close();
        statement.close();
        return false;
    } else {
        rs.close();
        statement.close();
        return true;
    }
    }
    public static boolean isExistCity(String city) throws UnsupportedEncodingException {
        String urlStr = URLEncoder.encode(city,"UTF-8");
        String  geturl = "https://qqlykm.cn/api/yiqing/?key=6i9Khjgl7RlqRQHHU0tmhGknvg&city="+urlStr;
        String result1 = HttpUtil.get(geturl);
        String result2 = result1.substring(0,result1.length()-1).replace("{\"success\":true,\"data\":","");
        Message message_1 =  JSONUtil.toBean(result2, Message.class);
        if(message_1.getConNum()==null){
            return true;
        }else {
            return false;
        }
    }

    public static boolean isNew(String data) throws SQLException {
        String sql = "SELECT updateTime from information where " +"locate("+"\""+data+"\""+",updateTime)";
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        Message message_2 = new Message();
        while(rs.next()){
            message_2.setUpdateTime(rs.getString("updateTime"));
        }
        if(message_2.getUpdateTime()!=null){

            rs.close();
            statement.close();
            return true;
        }
        else {
            rs.close();
            statement.close();
            return false;
        }

    }


}
